﻿using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Unity
{
    class Program
    {
        static void Main(string[] args)
        {
            ////依赖注入生命周期：Register，Resolve，Dispose。
            using (IUnityContainer containerA = new UnityContainer())
            {
                using (IUnityContainer containerB = new UnityContainer())
                {
                    containerA.RegisterType<IJob, Programmer>("程序员")//注册对象映射
                        .RegisterType<IJob, Student>("学生")
                        //.RegisterInstance<IDateTime>(new Millisecond())//单例模式的注册
                        .RegisterType<IDateTime, Millisecond>();

                    Console.WriteLine(containerA.Resolve<IJob>("程序员").GetName());
                    Console.WriteLine(containerA.Resolve<IJob>("学生").GetName());
                    Console.WriteLine("{0}毫秒。", containerA.Resolve<IDateTime>().ShowMillisecond());
                    Thread.Sleep(100);
                    Console.WriteLine("{0}毫秒。", containerA.Resolve<IDateTime>().ShowMillisecond());

                    //通过配置注册对象映射
                    UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);
                    configuration.Configure(containerB, "defaultContainer");
                    var management = containerB.Resolve<Management>();
                    Console.WriteLine("构造函数注入：{0}", management.InjectionConstructor.GetName());
                    Console.WriteLine("属性注入：{0}", management.InjectionProperty.ShowMillisecond());
                    Console.WriteLine("方法注入：{0}", management.InjectionMethod.ShowMillisecond());
                    Console.WriteLine(ReferenceEquals(management.InjectionMethod, management.InjectionProperty));
                    Console.ReadKey(true);
                }
            }

        }
    }

    interface IJob
    {
        string GetName();
    }

    class Programmer : IJob
    {

        public string GetName()
        {
            return "我是程序员！";
        }
    }

    class Student : IJob
    {

        public string GetName()
        {
            return "我是学生！";
        }
    }

    class Management
    {
        [Dependency]
        public IDateTime InjectionProperty { get; set; }

        public IDateTime InjectionMethod { get; set; }
        public IJob InjectionConstructor { get; set; }


        public Management(IJob job)
        {
            this.InjectionConstructor = job;
        }

        [InjectionMethod]
        public void Method(IDateTime dateTime)
        {
            this.InjectionMethod = dateTime;
        }

    }

    interface IDateTime
    {
        string ShowMillisecond();
    }

    class Millisecond : IDateTime
    {
        private string _millisecond;

        public Millisecond()
        {
            _millisecond = DateTime.Now.Millisecond.ToString();
        }
        public string ShowMillisecond()
        {
            return _millisecond;
        }
    }

}
